<template>
    <div>
      <el-card style="width: 40%">
        <el-form :model="user" label-width="80px" style="padding-right: 20px">
          <div style="margin: 15px; text-align: center">
            <el-upload
                class="avatar-uploader"
                :action="'http://localhost:9090/api/files/upload'"
                :headers="{ token: user.token }"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
            >
              <img v-if="user.img" :src="'http://localhost:9090/api/files/'+ user.img" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="user.username" placeholder="用户名" disabled></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="user.name" placeholder="姓名"></el-input>
          </el-form-item>
          <el-form-item label="年龄" prop="age">
            <el-input v-model="user.age" placeholder="年龄"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="user.phone" placeholder="电话"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="user.sex">
              <el-radio label="男">男</el-radio>
              <el-radio label="女">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <div style="text-align: center; margin-bottom: 20px">
            <el-button type="primary" @click="update">保 存</el-button>
          </div>
        </el-form>
      </el-card>
    </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Person",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('user') || '{}')
    }
  },
  created() {

  },
  methods: {
    update(){
        request.post('/admin',this.user).then(res =>{
            if (res.code === '200'){
              // 成功更新
              this.$message.success('保存成功')

              // 更新浏览器缓存里的用户信息
              localStorage.setItem('user', JSON.stringify(this.user))

              // 触发父级的数据更新
              this.$emit('update:user', this.user)
            }else {
              this.$message.error(res.msg)
            }
        })
    },
    handleAvatarSuccess(response, file, fileList) {
      // 把user的头像属性换成上传的图片的链接
      this.user.img = response.data
    },
  }
}
</script>

<style scoped>
/deep/ .el-form-item__label{
  font-weight: bold;
}
/deep/.el-form-item__label {
  font-weight: bold;
}
/deep/.el-upload {
  border-radius: 50%;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}
/deep/.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border-radius: 50%;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border-radius: 50%;
}
</style>